package edu.ua.cs.cs515.gcj.tetris;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;

/**
 * The Main GUI component.  Creates the tetris model and the tetris view and
 * attached the view to the model.  Creates a tetris controller and attaches the
 * controller to the view.
 */
public class Main extends JFrame implements ActionListener {
	
	// Our tetris model
	private TetrisModel tetrisModel;
	
	public static JMenu difficultyMenu = new JMenu("Select Difficulty");
	
	private JRadioButtonMenuItem easyItem;
	private JRadioButtonMenuItem mediumItem;
	private JRadioButtonMenuItem hardItem;
	private JRadioButtonMenuItem insaneItem;
	private JMenuItem endGameItem;
	private JMenuItem exitItem;
	private JRadioButtonMenuItem soundItem;

	private TetrisController controller = new TetrisController();
	
	public static void main(String[] args) {
		Main main = new Main();
		main.setVisible(true);
	}
	
	public Main() {
		JMenuBar menuBar = new JMenuBar();
		// Create the tetris view and attach the controller
		TetrisView tetrisView = new TetrisView(controller);
		// Create the tetris model and attach the view
		tetrisModel = new TetrisModel(tetrisView);
		// Set the model of the controller to our tetris model
		controller.setModel(tetrisModel);
		// Add the view to the GUI
		this.add(tetrisView);
		this.setSize(new Dimension(600,660));
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				
		easyItem = new JRadioButtonMenuItem("Easy");
		mediumItem = new JRadioButtonMenuItem("Medium");
		hardItem = new JRadioButtonMenuItem("Hard");
		insaneItem = new JRadioButtonMenuItem("Insane");
		
		mediumItem.setSelected(true);
		
		easyItem.addActionListener(this);
		mediumItem.addActionListener(this);
		hardItem.addActionListener(this);
		insaneItem.addActionListener(this);
		
		ButtonGroup group = new ButtonGroup();
		group.add(easyItem);
		group.add(mediumItem);
		group.add(hardItem);
		group.add(insaneItem);
		
		difficultyMenu.add(easyItem);
		difficultyMenu.add(mediumItem);
		difficultyMenu.add(hardItem);
		difficultyMenu.add(insaneItem);
		
		JMenu gameMenu = new JMenu("Game");
		
		soundItem = new JRadioButtonMenuItem("Sound");
		endGameItem = new JMenuItem("End Game");
		exitItem = new JMenuItem("Exit");
		soundItem.addActionListener(this);
		endGameItem.addActionListener(this);
		exitItem.addActionListener(this);
		soundItem.setSelected(true);
		gameMenu.add(soundItem);
		gameMenu.add(endGameItem);
		gameMenu.add(exitItem);
		
		menuBar.add(gameMenu);
		menuBar.add(difficultyMenu);
		
		gameMenu.setBackground(Color.WHITE);
		difficultyMenu.setBackground(Color.WHITE);
		menuBar.setBackground(Color.WHITE);

		this.setJMenuBar(menuBar);
		
		setTitle("Tetris");
		URL url = this.getClass().getResource("/edu/ua/cs/cs515/gcj/tetris/res/icon.png");
		Toolkit kit = Toolkit.getDefaultToolkit();
		Image img = kit.createImage(url);
		setIconImage(img);

		this.setJMenuBar(menuBar);
		tetrisModel.initializeGame();
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == easyItem) {
			 controller.setDifficulty('E');
		} else if (evt.getSource() == mediumItem) {
			controller.setDifficulty('M');
		} else if (evt.getSource() == hardItem) {
			controller.setDifficulty('H');
		} else if (evt.getSource() == insaneItem) {
			controller.setDifficulty('I');
		} else if (evt.getSource() == endGameItem) {
			difficultyMenu.setEnabled(true);
			if (controller.isGameRunning()) {
				int score = controller.getScore();
				controller.endGame();
				if (score > 0) {
					JOptionPane.showMessageDialog(this, "Congratulation!\nYou scored " + score + " points!", "Game Over", JOptionPane.PLAIN_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(this, "Bummer!\nYou didn't score any points!", "Game Over", JOptionPane.PLAIN_MESSAGE);
				}
				soundItem.setEnabled(true);
			}
		} else if (evt.getSource() == exitItem) {
			controller.endGame();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.exit(0);
		} else if (evt.getSource() == soundItem) {
			System.out.println(!soundItem.isSelected());
			controller.muteSound(!soundItem.isSelected());
		}
	}

}
